<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
<!-- main home -->
<div id="main">
    <div id="main_content" class="page_new">
        <!-- main grid -->
        <div class="dlb mt10">
            <h1 class="fll stick">NEW OF TODAY</h1>
            <ul class="trend_tabs">
                <li><a class="selected" href="#">All</a></li>
                <li><a href="#">Street</a></li>
                <li><a href="#">Vintage</a></li>
                <li><a href="#">Trend 1</a></li>
                <li><a href="#">Trend 2</a></li>
                <li><a href="#">Trend 3</a></li>
                <li><a href="#">Trend 4</a></li>
                <li id="trend-expand">
                    <a href="">Khác</a>
                    <div class="trend-ex-panel">
                        <a href="" class="tep_a" title="#">Trend 5</a>
                        <a href="" class="tep_a" title="#">Trend 6</a>
                        <a href="" class="tep_a" title="#">Trend 7</a>
                    </div>
                </li>
            </ul>
            <div class="clr"></div>
        </div>
        <div class="grid_mask mt20">
            <div class="grid">
                <section>
                    <ul class="items">
                        <?php
                        if ($model != null) {
                            foreach ($model as $i) {
                                $checkFollow = UserFollow::model()->find("followed = :followed and follower =:follower", array(
                                    ':followed' => $i->userCreated->id,
                                    'follower' => Yii::app()->user->id
                                ));
                                ?>
                                <li class="item">
                                    <div class="item_thumb">
                                        <div class="item_header">
                                            <img src="<?php echo Yii::app()->baseUrl . '/upload/user/' . $i->userCreated->id . '/avatar/' . $i->userCreated->avatar; ?>" border="0" class="ih_avatar" />
                                            <div class="ih_info">
                                                <a href="<?php echo Yii::app()->createUrl('user/view', array('username' => $i->userCreated->username)); ?>" class="ih_title" title="<?php echo $i->userCreated->fullname; ?>"><?php echo $i->userCreated->fullname; ?></a>
                                                <p><?php echo Format::timeAgo($i->created_at, 'Y-m-d H:i:s'); ?></p>
                                            </div>
                                            <?php if (!$checkFollow && Yii::app()->user->id != null && Yii::app()->user->id != $i->userCreated->id) {
                                                ?>                                
                                                <a id="user_<?php echo $i->userCreated->id; ?>" onclick="return follow(<?php echo $i->userCreated->id; ?>);" href="#" title="Follow" class="ih_follow effect">Theo dõi</a>
                                            <?php } ?>
                                        </div>
                                        <a href="#" title="Thích" class="pose_like"></a>
                                        <div class="item_bottom">
                                            <div class="ib_liked pose_icons"><?php echo $i->like_cnt ?></div>
                                            <div class="ib_comment pose_icons"><?php echo sizeof(Comment::model()->findAll('image_id=:id', array(':id' => $i->id))); ?></div>
                                            <a href="#" title="Thêm vào bộ sưu tập" class="ib_save pose_icons"></a>
                                        </div>
                                        <a href="<?php echo Yii::app()->createUrl('image/view', array('id' => $i->id)); ?>"><img src="<?php echo Yii::app()->baseUrl . '/upload/user/' . $i->userCreated->id . '/images/' . $i->img_url; ?>" border="0" /></a>
                                    </div>
                                    <div class="item_info">
                                        <a href="<?php echo Yii::app()->createUrl('image/view', array('id' => $i->id)); ?>"><h1><?php echo $i->title; ?></h1></a>
                                    </div>
                                </li>
                                <?php
                                $rate = $i->like_cnt * 5 + $i->view_cnt;
                            }
                            if (sizeof($model) >= 12) {
                                ?>
                                <li class="item">
                                    <div class="item_more" id="<?php echo $rate; ?>">
                                        <a href="#" onclick="return loadmore();" title="Xem thêm"><span>+</span><div style="margin-top: -20px;">Xem thêm</div></a>
                                    </div>
                                </li>
                                <?php
                            }
                        } else {
                            echo "<h2><i>No results found.</i></h2>";
                        }
                        ?>
                    </ul>
                </section>
                <div class="clr"></div>
            </div>
        </div>
        <!-- / main grid -->
    </div>
</div>
<!-- main home -->
<script type="text/javascript">
                                        function like(id) {
                                            $.ajax({
                                                url: "<?php echo Yii::app()->createUrl('image/like') . '?id='; ?>" + id,
                                                type: 'GET',
                                                success: function(data) {
                                                    var json = eval('(' + data + ')');
                                                    $("a.like_link_" + id).text(json['meg']);
                                                    $("p.like_cnt_" + id).text(json['like_cnt']);
                                                }
                                            });
                                            return false;
                                        }
                                        function follow(id) {
                                            $.ajax({
                                                url: "<?php echo Yii::app()->createUrl('user/follow') . '?id='; ?>" + id,
                                                type: 'GET',
                                                success: function() {
//                                                                $("#user_" + id).remove();
                                                    $('a[id*=user_' + id + ']').remove();
                                                }
                                            });
                                            return false;
                                        }
                                        function loadmore() {
                                            var rate = $(".item_more").attr('id');
                                            $.ajax({
                                                url: "<?php echo Yii::app()->createUrl('site/loadmore') . '?id='; ?>" + rate,
                                                type: 'GET',
                                                success: function(data) {
                                                    var json = eval('(' + data + ')');
                                                    for (var i = 0; i < json.length; i++) {
                                                        sdata = '';
                                                        sdata += '<li class="item" style="position: absolute; display: list-item; top: 0px; left: 735px;"><div class="item_thumb"><div class="item_header">';
                                                        sdata += '<img src="<?php echo Yii::app()->baseUrl . '/upload/user/'; ?>' + json[i].user_created + '/avatar/' + json[i].avatar + '" border="0" class="ih_avatar" />';
                                                        sdata += '<div class="ih_info">';
                                                        sdata += '<a href="' + json[i].user_url + '" class="ih_title" title="' + json[i].title + '">' + json[i].fullname + '</a>';
                                                        sdata += '<p>' + json[i].time + '</p></div>';
                                                        if (json[i].isFollowed) {
                                                            sdata += '<a id="user_' + json[i].user_created + '" onclick="return follow(' + json[i].user_created + ');" href="#" title="Follow" class="ih_follow effect">Theo dõi</a>';
                                                        }
                                                        sdata += '</div>';
                                                        sdata += '<a href="#" title="Thích" class="pose_like"></a>';
                                                        sdata += '<div class="item_bottom">';
                                                        sdata += '<div class="ib_liked pose_icons">' + json[i].like_cnt + '</div>';
                                                        sdata += '<div class="ib_comment pose_icons">' + json[i].comment_cnt + '</div>';
                                                        sdata += '<a href="#" title="Thêm vào bộ sưu tập" class="ib_save pose_icons"></a></div>';
                                                        sdata += '<a href="' + json[i].view_url + '"><img src="<?php echo Yii::app()->baseUrl . '/upload/user/'; ?>' + json[i].user_created + '/images/' + json[i].img_url + '" border="0" /></a>';
                                                        sdata += '</div><div class="item_info">';
                                                        sdata += '<a href="' + json[i].view_url + '"><h1>' + json[i].title + '</h1></a></div></li>';
//                                                                    $("li.item:last").after(sdata);
//                                                        $("ul.items").append(sdata);
                                                        $(sdata).hide().insertAfter("li.item:last").slideDown(400);
                                                    }
                                                }
                                            });
                                            return false;
                                        }
</script>
